﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class chi_tiet_tin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(Request.QueryString["BaiVietID"]))
            Response.Redirect("Error.aspx");
        if (!IsPostBack)
        {
            CheckLogin();
            GetPostDetail();
            GetComment();
            Get5OtherNews();
            GetTopNews();
            GetTopPoster();
            UpdatePostViews();
        }
    }
    protected void GetPostDetail()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetPostDetailByPostID";
        Dal.ParameterCollection = new string[1] { "@BaiVietID"};
        Dal.ValueCollection = new string[1] { Request.QueryString["BaiVietID"].ToString()};
        DataRow dr = Dal.GetDataRow();
        lblCountComment.Text = "("+dr["LuotBinhLuan"].ToString()+")";
        lblCountViews.Text = "("+dr["LuotXem"].ToString()+")";
        lblDatePost.Text = dr["NgayDang"].ToString();
        lblPostTitle.Text =dr["TieuDe"].ToString();
        lblPostTopic.Text = dr["TenChuDe"].ToString();
        lblPostUserEmail.Text = dr["TenHienThi"].ToString();
        Page.Title = "Tin tức | "+dr["TieuDe"].ToString();
        ltrPostContent.Text = dr["NoiDung"].ToString();
    }
    protected void GetComment()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetCommentByPostId";
        Dal.ParameterCollection = new string[1] { "@BaiVietID" };
        Dal.ValueCollection = new string[1] { Request.QueryString["BaiVietID"].ToString() };
        Repeater1.DataSource = Dal.GetDataTable();
        Repeater1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_AddPostComment";
        Dal.ParameterCollection = new string[4] { "@BaiVietID", "@UserEmail", "@NoiDung", "@NgayDang" };
        Dal.ValueCollection = new string[4] { Request.QueryString["BaiVietID"].ToString(), Session["UserEmail"].ToString(), TextBox1.Text, DateTime.Today.ToString() };
        Dal.ExecuteNonQuery();
        Dal.Close();
        Dal.Dispose();
        TextBox1.Text = "";
        GetComment();
    }
    protected void Get5OtherNews()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_Get5OtherNewsByTopicID";
        Dal.ParameterCollection = new string[1] { "@BaiVietID"};
        Dal.ValueCollection = new string[1] { Request.QueryString["BaiVietID"].ToString()};
        rptOtherNews.DataSource = Dal.GetDataTable();
        rptOtherNews.DataBind();
    }
    protected void GetTopNews()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetTop5NewestNews";
        DataTable db = Dal.GetDataTable();
        rptTopHotNews.DataSource = db;
        rptTopHotNews.DataBind();
    }
    protected void GetTopPoster()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetTopPoster";
        DataTable db = Dal.GetDataTable();
        rptTopPoster.DataSource = db;
        rptTopPoster.DataBind();
    }
    protected void UpdatePostViews()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_Update_PostViews";
            Dal.ParameterCollection = new string[1] { "@BaiVietID" };
            Dal.ValueCollection = new string[1] { Request.QueryString["BaiVietID"].ToString() };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetPostDetail();
        }
        catch(Exception ex)
        {
            //Response.Redirect("Error.aspx");
            Response.Write(ex.Message);
        }
        
    }
    protected void CheckLogin()
    {
        if (Session["UserEmail"] == null)
        {
            Button1.Enabled = false;
            Button1.ToolTip = "Bạn cần đăng nhập để bình luận";
            TextBox1.Text = "Bạn cần đăng nhập để bình luận";
        }

    }
}
